﻿/******************************************************************************
 *  作者：       Maxzhang1985
 *  创建时间：   2012/5/2 10:22:33
 *
 *
 ******************************************************************************/
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading;
using System.Xml.Serialization;
using System.ComponentModel;
using System.Collections.ObjectModel;
using System.Xml.Serialization;
using ExtendPropertyLib.WPF;
using ExtendPropertyLib;

namespace ExORMDesginer.Plugin
{
    [Serializable]
    public class DbEntity:BusinessInfoBase<DbEntity>
    {
        public DbEntity()
        {
            Propertys = new SerializableObservableCollection<DbProperty>();
        }

        [XmlAttribute("ID")]
        public Guid ID { set; get; }
       
        public static ExtendProperty NameProperty = RegisterProperty<DbEntity>(v => v.Name);
        /// <summary>
        /// 名称
        /// </summary>
        [XmlAttribute("Name")]
        public string Name { set { SetValue(NameProperty, value); } get { return (string)GetValue(NameProperty); } }

        
        public static ExtendProperty DbNameProperty = RegisterProperty(v => v.DbName);
        /// <summary>
        /// 数据库中的名称
        /// </summary>
        public string DbName { set { SetValue(DbNameProperty, value); } get { return (string)GetValue(DbNameProperty); } }
       
        public static ExtendProperty MemoProperty = RegisterProperty<DbEntity>(v => v.Memo);
        /// <summary>
        /// 说明
        /// </summary>
        [XmlAttribute("Memo")]
        public string Memo { set { SetValue(MemoProperty, value); } get { return (string)GetValue(MemoProperty); } }

        
        public static ExtendProperty EntityViewTypeProperty = RegisterProperty(v => v.EntityViewType);
        /// <summary>
        /// 实体类型
        /// </summary>
        public EntityType EntityViewType { set { SetValue(EntityViewTypeProperty, value); } get { return (EntityType)GetValue(EntityViewTypeProperty); } }
      
        public static ExtendProperty DisplayNameProperty = RegisterProperty(v => v.DisplayName);
        /// <summary>
        /// 显示名称，如果memo为空值 则显示name值
        /// </summary>
        [XmlIgnore]
        public string DisplayName
        {
            set
            {
                SetValue(MemoProperty, value);
                SetValue(DisplayNameProperty, value);
            }
            get
            {
                return string.IsNullOrEmpty(Memo) ? Name : Memo;
            }
        }

       
        public static ExtendProperty KeysProperty = RegisterProperty<DbEntity>(v => v.Keys);
        /// <summary>
        /// 主键集合
        /// </summary>
        [XmlAttribute("Keys")]
        public string Keys { set { SetValue(KeysProperty, value); } get { return (string)GetValue(KeysProperty); } }

        public string PrimaryKeys
        {
            get
            {
                var keys = Propertys.Where(p => p.IsPrimaryKey).Select(p => string.Format("\"{0}\"", p.Name));
                return string.Join(",", keys);
            }
        }

       
        public static ExtendProperty PropertysProperty = RegisterProperty<DbEntity>(v => v.Propertys,ObjectRelations.Children);
        /// <summary>
        /// 字段属性集合
        /// </summary>
        [XmlArray("Propertys")]
        public ObservableCollection<DbProperty> Propertys { set { SetValue(PropertysProperty, value); } get { return (ObservableCollection<DbProperty>)GetValue(PropertysProperty); } }



        
        public static ExtendProperty SQLProperty = RegisterProperty(v => v.SQL);
        public string SQL { set { SetValue(SQLProperty, value); } get { return (string)GetValue(SQLProperty); } }
   


      
    }
}
